//
//  StuffViewController.m
//  Menu
//
//  Created by YouLu on 15/7/3.
//  Copyright (c) 2015年 lijiangang. All rights reserved.
//

#import "StuffViewController.h"

@interface StuffViewController ()

@end

@implementation StuffViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.picArray = [NSMutableArray array];
    self.nameArray = [NSMutableArray arrayWithObjects:@"", nil];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.view.frame =CGRectMake(0, 64+260*HEIGHT+40*HEIGHT, MyWidth, MyHeight-64-49-260*HEIGHT-40*HEIGHT);;
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, MyWidth, MyHeight-64-49-260*HEIGHT-40*HEIGHT) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorStyle =UITableViewCellSeparatorStyleNone;
    self.tableView.bounces = YES;
    [self.view addSubview:self.tableView];
    [_tableView release];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"loading";
    [self.HUD show:YES];
    
}
-(void)loadData:(NSString *)vegetableId
{
    

    NSString *strUrl = [NSString stringWithFormat:@"http://121.41.117.95:80/HandheldKitchen/api/vegetable/tblvegetable!getTblVegetables.do?vegetable_id=%@&phonetype=2&user_id=&is_traditional=0",vegetableId];
    [AFNTool get:strUrl Body:nil HttpResult:JSON success:^(id result) {
        
        NSMutableArray *dataArray = [result objectForKey:@"data"];
        NSMutableDictionary *dic = [dataArray objectAtIndex:0];
        self.stuffModel = [[StuffModel alloc] initWithDic:dic];
        [self lableText];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
        self.HUD.labelText = @"加载失败";
        [self.HUD hide:YES afterDelay:1];
        
    }];
    
    NSString *picStrUrl = [NSString stringWithFormat:@"http://121.41.117.95:80/HandheldKitchen/api/vegetable/tblvegetable!getIntelligentChoice.do?vegetable_id=%@&type=1&phonetype=0&is_traditional=0",vegetableId];
    
    [AFNetWorkingYU netWorkingHanderGetGataWithUrl:picStrUrl resultBlock:^(id result) {
        NSMutableArray *dataArray = [result objectForKey:@"data"];
        NSMutableDictionary *lastDic = [dataArray lastObject];
        NSString *stockPic = [lastDic objectForKey:@"imagePath"];
        
        [self.picArray addObject:stockPic];
        NSMutableArray *tblSeasoningArray = [lastDic objectForKey:@"TblSeasoning"];
        for (NSMutableDictionary *dic in tblSeasoningArray) {
            NSString *seasoningPic = [dic objectForKey:@"imagePath"];
            NSString *nameStr = [dic objectForKey:@"name"];
            [self.nameArray addObject:nameStr];
            [self.picArray addObject:seasoningPic];
        }
        [self.tableView reloadData];
        [self.HUD hide:YES];
    }];
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.picArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    StuffTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[StuffTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.stuffmodel = self.stuffModel;
    cell.picUrl = [self.picArray objectAtIndex:indexPath.row];
    cell.nameStr = [self.nameArray objectAtIndex:indexPath.row];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 210*HEIGHT;
}
-(void)lableText
{
    
    NSString *stockStr = [NSString stringWithFormat:@"原料: %@",self.stuffModel.fittingRestriction];
    NSString *seasoningStr = [NSString stringWithFormat:@"调料: %@",self.stuffModel.method];
    CGFloat stockHeight = [[self class] heightForcell:stockStr];
    CGFloat seasoningHeight = [[self class] heightForcell:seasoningStr];
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MyWidth, stockHeight+seasoningHeight+20*HEIGHT)];
    
    self.stockLabel = [[UILabel alloc] initWithFrame:CGRectMake(40*WIDTH, 10*HEIGHT, 295*WIDTH, stockHeight)];
    self.stockLabel.text = stockStr;
    self.stockLabel.numberOfLines = 0;
    [self.tableView.tableHeaderView addSubview:self.stockLabel];
    [_stockLabel release];
    
    self.seasoningLabel = [[UILabel alloc] initWithFrame:CGRectMake(40*WIDTH, 10*HEIGHT+stockHeight, 295*WIDTH, seasoningHeight)];
    self.seasoningLabel.text = seasoningStr;
    self.seasoningLabel.numberOfLines = 0;
    [self.tableView.tableHeaderView addSubview:self.seasoningLabel];
    [_seasoningLabel release];
    
}
+(CGFloat)heightForcell:(NSString *)content
{
    //参数1.设置计算内容的宽和高
    //参数2.设置计算高度模式
    //参数3.设置计算字体大小属性
    //参数4.系统备用参数,设置为nil
    //其中宽度一定与显示内容的label宽度一致否则计算不准确  高度尽量大一些
    CGSize size=CGSizeMake(295*WIDTH, 10000);
    //字体大小一定与显示内容的label字体大小一致  默认是17.0
    NSDictionary *dic =[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:17.0] forKey:NSFontAttributeName];
    CGRect frame=  [content boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return frame.size.height;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
